package com.app.micaihu.view.home.knowledge.adapter

import android.util.TypedValue
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.app.micaihu.R
import com.app.micaihu.bean.news.NewsEntity
import com.app.micaihu.configure.Config
import com.app.micaihu.custom.view.RatingBarView
import com.app.micaihu.utils.StatServiceUtils
import com.app.micaihu.videoplayer.JCUserAction
import com.app.micaihu.videoplayer.JCUserActionStandard
import com.app.micaihu.videoplayer.JCVideoPlayer
import com.app.micaihu.videoplayer.JCVideoPlayerStandard
import com.app.micaihu.view.utils.loadCircleImage
import com.app.micaihu.view.utils.loadImage
import com.app.utils.util.StringUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * 知识 适配器
 */
class KnowledgeListAdapter : BaseQuickAdapter<NewsEntity, BaseViewHolder>(R.layout.knowledge_item_video_list), LoadMoreModule, JCUserActionStandard {

    override fun convert(holder: BaseViewHolder, item: NewsEntity) {
        StatServiceUtils.event(Config.EventBauduId.knowledgelist_exposure, "知识视频曝光")

        holder.getView<ImageView>(R.id.ivAuthorImg).loadCircleImage(item.authorHeadPic, R.drawable.user_head_def)

        holder.setText(R.id.tvPlayNum, item.clickNum + "播放")
        val jcVideoPlayer = holder.getView<JCVideoPlayerStandard>(R.id.item_videoplayer)
        jcVideoPlayer.setWidthRatio(2)
        jcVideoPlayer.setHeightRatio(1)
        jcVideoPlayer.setJcUserAction(this)
        holder.setText(R.id.tvAuthor, item.authorName)
        //加载视频
        jcVideoPlayer.setShowTitle(true)

        if (item.articleThumb != null && item.articleThumb.isNotEmpty()) {
            jcVideoPlayer.setUp(item.videoCurl, JCVideoPlayer.SCREEN_LAYOUT_LIST,
                    item.articleTitle, item.articleThumb[0])
        } else {
            jcVideoPlayer.setUp(item.videoCurl, JCVideoPlayer.SCREEN_LAYOUT_LIST,
                    item.articleTitle, "")
        }
        jcVideoPlayer.setOnClickListener {

        }
        jcVideoPlayer.setShareBean(item, item.urlStatus)
        val gameInfo = item.gameInfo
        if (gameInfo == null) {
            jcVideoPlayer.setOnVideoListener(null)
            holder.setVisible(R.id.llBottom, true)
            holder.setGone(R.id.llStart, true)
        } else {
            jcVideoPlayer.setOnVideoListener(object : JCVideoPlayer.OnVideoListener {
                override fun onPlayClick() {
                    StatServiceUtils.event(Config.EventBauduId.knowledgelist_click, "知识视频点击")
                }

                override fun onProgress(seconds: Int) {
                    if (seconds > 3 && !item.isThreeSecondDisplay) {
                        StatServiceUtils.event(Config.EventBauduId.knowledgelist_gameexposure, "知识列表游戏曝光")
                        item.isThreeSecondDisplay = true
                        holder.setVisible(R.id.llStart, true)
                        holder.setGone(R.id.llBottom, true)
                    }
                }
            })
            holder.getView<ImageView>(R.id.ivGameIcon).loadImage(gameInfo.appIcon)
            holder.setText(R.id.tvGameName, gameInfo.title)
            holder.setText(R.id.tvGameScore, gameInfo.gradeScore)
            holder.getView<RatingBarView>(R.id.rbScore).setStar(StringUtils.toFloat(gameInfo.gradeScore, 0.0f) / 2)
            val llTag = holder.getView<LinearLayout>(R.id.llTag)
            llTag.removeAllViews()
            for ((index, game) in gameInfo.appType.withIndex()) {
                val textView = TextView(context)
                val layoutParams: LinearLayout.LayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                layoutParams.rightMargin = SizeUtils.dp2px(10f)
                textView.layoutParams = layoutParams
                textView.text = game
                textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9f)
                textView.setTextColor(ColorUtils.getColor(R.color.white))
                textView.setPadding(SizeUtils.dp2px(10f), SizeUtils.dp2px(2f), SizeUtils.dp2px(10f), SizeUtils.dp2px(2f))
                when (index) {
                    0 -> {
                        textView.setBackgroundResource(R.drawable.knowledge_game_tag_bg1)
                    }
                    1 -> {
                        textView.setBackgroundResource(R.drawable.knowledge_game_tag_bg2)
                    }
                    2 -> {
                        textView.setBackgroundResource(R.drawable.knowledge_game_tag_bg3)
                    }
                    else -> {
                        textView.setBackgroundResource(R.drawable.knowledge_game_tag_bg1)
                    }
                }
                llTag.addView(textView)
            }
        }
        if (item.isThreeSecondDisplay) {
            holder.setVisible(R.id.llStart, true)
            holder.setGone(R.id.llBottom, true)
        } else {
            holder.setVisible(R.id.llBottom, true)
            holder.setGone(R.id.llStart, true)
        }
    }

    override fun onEvent(type: Int, url: String?, screen: Int, vararg objects: Any?) {
        if (type == JCUserAction.ON_CLICK_START_ICON || type == JCUserActionStandard.ON_CLICK_START_THUMB) {
//            if (tvPlayNum != null && tvPlayNum?.visibility == View.VISIBLE) {
//                this.tvPlayNum?.visibility = View.GONE
//            }
        }
    }


}